#!/bin/sh
## ./CAutoCompile.sh Debug  or  ./CAutoCompile.sh Release

############################Begin Input Para#############################
build_type=$1
printf 'build_type=%s\n' $build_type
if [[ $build_type == '' ]]
then
    build_type='Debug'
fi

final_build_type=' ' 
if [[ $build_type == 'Debug' ]]
then
    final_build_type='Debug'
else
    final_build_type='Release'
fi
printf 'final_build_type=%s\n' $final_build_type

cur_path=$(pwd)
printf 'cur_path=%s\n' $cur_path

build_path=$cur_path/build
printf 'build_path=%s\n' $build_path
############################End Input Para###############################

############################Begin Operate Dir#############################
if [[ ! -d $build_path ]]
then
    mkdir $build_path
    printf 'build path is not existed and create it now\n'
else
    printf 'build path is existed\n'
    if [[ $build_type == 'Debug' ]]
    then
        printf 'keep Release directory when compiling\n'
    else
        printf 'keep Debug directory when compiling\n'
    fi
    cmake_file_dir=$build_path/CMakeFiles
    cmake_cache_file=$build_path/CMakeCache.txt
    camke_install_file=$build_path/cmake_install.cmake
    cmake_dir=$build_path/.cmake
    rm -rf $cmake_file_dir
    rm -rf $cmake_cache_file
    rm -rf $camke_install_file
    rm -rf $cmake_dir
fi
############################End Operate Dir###############################

############################Begin Compile Prj###############################
cd $build_path
if [[ $build_type == 'Debug' ]]
    then
        printf 'compile debug\n'
        cmake -DCMAKE_BUILD_TYPE=Debug ../
        make -j3
    else
        printf 'compile release\n'
        cmake -DCMAKE_BUILD_TYPE=Release ../
        make -j3
fi
############################End Compile Prj###############################

############################Begin Copy Dependency###############################
cd $build_path/../
dest_dir=$build_path/$build_type
printf 'dest_dir=%s\n' $dest_dir

# OpenSSL
openssl_lib_dir=$cur_path/ShareLib/Unix/OpenSSL/lib
printf 'copy openssl from %s\n' $openssl_lib_dir
cp -rf $openssl_lib_dir/* $dest_dir

# Poco
poco_dir=$cur_path/ShareLib/Unix/Poco/lib
poco_lib_dir=$poco_dir/$build_type
printf 'copy poco from %s\n' $poco_lib_dir
cp -rf $poco_lib_dir/* $dest_dir
############################End Copy Dependency#################################

############################Begin Compile test###############################
test_build_path=$cur_path/UnitTest/build

if [[ ! -d $test_build_path ]]
then
    mkdir $test_build_path
    printf 'test build path is not existed and create it now\n'
else
    printf 'build path is existed\n'
    if [[ $build_type == 'Debug' ]]
    then
        printf 'keep test release directory when compiling\n'
    else
        printf 'keep test debug directory when compiling\n'
    fi
    cmake_file_dir=$test_build_path/CMakeFiles
    cmake_cache_file=$test_build_path/CMakeCache.txt
    camke_install_file=$test_build_path/cmake_install.cmake
    cmake_dir=$test_build_path/.cmake
    rm -rf $cmake_file_dir
    rm -rf $cmake_cache_file
    rm -rf $camke_install_file
    rm -rf $cmake_dir
fi

cd $test_build_path
if [[ $build_type == 'Debug' ]]
    then
        printf 'compile test debug\n'
        cmake -DCMAKE_BUILD_TYPE=Debug ../
        make -j3
    else
        printf 'compile test release\n'
        cmake -DCMAKE_BUILD_TYPE=Release ../
        make -j3
fi

cd $test_build_path/../../
test_dest_dir=$test_build_path/$build_type
printf 'test dest_dir=%s\n' $test_dest_dir
if [[ ! -d $test_dest_dir ]]
then
    mkdir $test_dest_dir
fi

# BaseTool library
basetool_lib_dir=$dest_dir
printf 'copy basetool from %s\n' $basetool_lib_dir
cp -rf $basetool_lib_dir/* $test_dest_dir
############################End Compile Test###############################
